#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import re
import time
import sys
import url
import codecs
import Logging as logging
import pymongo
import hashlib
import datetime



_encoding = 'utf-8'
_errors='ignore'

_host='10.103.13.14'
_db='statis'
_user='tongji'
_password='123456'

_mongohost='10.103.13.42'
_mongoport=27017

client_pid={}
android2_pid={}

path = sys.argv[1]
date = sys.argv[2]
dir_date = os.path.join(date[:4], date[4:6], date[6:8])
logpath = sys.argv[3]

def main():
    global loger
    global path
    global date
    global dir_date
    global logpath

    loger = logging.initlog('{0}{1}{2}{3}'.format(os.path.join(logpath), 'statis-user-', date, '.log'))
    loger.info('Start static_user.py  {0} {1} {2}'.format(path, date, logpath))
    stime = time.time()
    
    if not path :
        path = '/v3/LogTrees'
    
    #db=MySQLdb.connect(host=_host,db=_db,user=_user,passwd=_password)
    #c=db.cursor()
    
    connection = pymongo.Connection(_mongohost,_mongoport)
    db = connection.ClientUser
    collectionapi = db['User']
    dbwap = connection.WapUser
    collectionwap = dbwap['User']

    for parent, dirnames, filenames  in os.walk(path) :
        if dir_date not in parent :
            continue
        for filename in filenames :
            jsonb={}
            jsonb['date'] = date
            jsonb['pid'] = filename
            
            if 'go' in parent:
                continue
            elif 'api' in parent:
                continue
            elif 'ios' in parent:
                continue
            elif 'wap' in parent:
                jsonb['server'] = 'wap'
            elif 'init-statis' in parent:
                jsonb['server'] = 'api'
            else:
                continue
            
            f = codecs.open(os.path.join(parent, filename) , 'r', encoding=_encoding, errors=_errors)
            row = f.readline()
            _tmp_list_api = []
            _tmp_list_wap = []
            s_num_api = 0
            s_num_wap = 0
            while row:
                jsonb = {}
                #timestr = time.strftime("%Y-%m-%d", time.localtime())
                timeobj = datetime.datetime.strptime(date, '%Y%m%d')
                
                info = row.split('^_^')
                
                if jsonb['server'] == 'api':
                    if '/initial' not in info[2]:
                        row = f.readline()
                        continue 
                    
                    if filename == 'nopid' :
                        row = f.readline()
                        continue
                    
                    data = {
                            'mac':info[24],
                            'imei':info[17],
                            'deviceid':info[19],
                            'uuid':info[20],
                            'pid':filename,
                            'os':info[14],
                            'btype':info[13],
                            'brand':info[12],
                            'ver':info[22],
                            'operator':info[25],
                            }
                    for key,value in data.items():
                        if value=="#":
                            data[key] = ''
                        #if key == 'btype':
                        #    data['btype'] = url.urldecode(value)
                    
                    guid = '{mac}&{imei}&{deviceid}&{uuid}'.format(**data)
                    guid = hashlib.md5(guid).hexdigest()
                    
                    _hour = info[0]
                    timeobj = timeobj + datetime.timedelta(hours=int(_hour))
                    timestr = timeobj.strftime('%Y-%m-%d %H:00:00')
                    
                    jsonb = {"guid":guid, "brand":data["brand"],"pid":data["pid"], "client_ver" : "", "deviceid" : data["deviceid"], "btype" : url.urldecode(data["btype"]), "operator" : url.urldecode(data["operator"]), "os":data["os"], "os_ver" : "", "ver":data["ver"], "network":""}
                    jsonb['regtime'] = int(time.mktime(timestr.timetuple()))
                    jsonb['lasttime'] =  jsonb['regtime']
                    collectionapi.insert(jsonb)
                    
                    #try:
                    #    c.execute("""INSERT INTO `user`(guid, server, ctime, os, btype, brand, pid, ver) VALUES(%s, %s, %s, %s, %s, %s, %s, %s) """, (guid, 'api', timestr, data['os'], data['btype'], data['brand'], data['pid'], data['ver'] ))
                    #except MySQLdb.Error, e:
                        #loger.info("Error %d: %s" % (e.args[0], e.args[1]))
                        #loger.info('End static_user.py  run time: {0}s'.format(str(etime-stime)))
                    
                else:
                    cookie = info[10]
                    _hour = info[0]
                    timeobj = timeobj + datetime.timedelta(hours=int(_hour))
                    timestr = timeobj.strftime('%Y-%m-%d %H:00:00')
                    
                    jsonb = {"cookie":cookie}
                    jsonb['regtime'] = int(time.mktime(timestr.timetuple()))
                    jsonb['lasttime'] =  jsonb['regtime']
                    
                    collectionwap.insert(jsonb)
                    
                    #try:
                    #    c.execute("""INSERT INTO `user`(cookie, server, ctime) VALUES(%s, %s, %s) """, (cookie, 'wap', timestr))
                    #except MySQLdb.Error, e:
                    #    loger.info("Error %d: %s" % (e.args[0], e.args[1]))
                                    
                row = f.readline()
            
    etime = time.time()
    loger.info('End static_user.py  run time: {0}s'.format(str(etime-stime)))

def new_user():
    global path
    global date
    global dir_date
    global logpath
    
    connection = pymongo.Connection("10.103.13.42",27017)
    db = connection.Clientuser
    collections = db['User']
    dbdaily = connection.DailyLog
    collectiondaily = dbdaily[date[:6]]
    
    stimeobj = datetime.datetime.strptime(date, '%Y%m%d')
    stime = stimeobj.strftime('%Y-%m-%d 00:00:00')
    etimeobj = stimeobj + datetime.timedelta(days=1)
    etime = etimeobj.strftime('%Y-%m-%d 00:00:00')
    istime = int(time.mktime(stime.timetuple()))
    ietime = int(time.mktime(etime.timetuple()))
    
    posts = collection.find({'server':'api', 'date':date})
    
    for post in posts: 
        pid = post['pid']
        #sql = "SELECT count(id) FROM user WHERE pid='{0}' and ctime>= '{1}' and ctime < '{2}'".format(pid, stime, etime)
        sql = {'regtime' : {'$gt': istime}, 'regtime' : {'$lt': ietime}}
        c = collections.find(sql)
        count = c.count()
        collectiondaily.update({'date':date, 'server':'api', 'pid' : pid}, {'$set':{'new_user':count}})

if __name__ == '__main__':
    main()
    new_user()